import { defineStore } from "pinia";
import { ref } from "vue";
import { useRouter } from 'vue-router'

export const useRouterStore = defineStore('router', () => {
  const router = useRouter()
  const routers = ref<RouteCache[]>([])
  const remove = (r: RouteCache | string, force = false) => {
    if (routers.value.length <= 1 && !force) return
    const p = typeof r !== 'string' ? r.path : r
    routers.value = routers.value.filter(i => i.path !== p)
    if (router.currentRoute.value.fullPath === p) {
      router.push(routers.value[0].path).then()
    }
  }
  const push = (r: RouteCache) => {
    if (!routers.value.find(i => i.path === r.path)) {
      routers.value = [...routers.value, r]
    }
  }
  const clear = () => {
    routers.value = []
  }
  return { routers, remove, push, clear }
})
